package com.zhang.pdf.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
 
/**
 * @Description 透明背景上色
 **/
public class PngColoringUtil {
    /**
     * @Description 给PNG图片增加背景色 返回BufferedImage
     * @param sourceImage 原始图片 最好是PNG透明的
     * @param backgroudColor 背景色
     * @return BufferedImage
     **/
    public static BufferedImage changeBGColor4PNG(BufferedImage image, Color backgroudColor) {
    	BufferedImage result = new BufferedImage(
                image.getWidth(),
                image.getHeight(),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D graphic = result.createGraphics();
        graphic.drawImage(image, 0, 0, backgroudColor, null);
        graphic.dispose();
        return result;
      }
    
    /**
     * @Description 给PNG图片增加背景色,并修改图片大小
     * @param sourceImage 原始图片 最好是PNG透明的
     * @param backgroudColor 背景色
     * @return BufferedImage
     **/
    public static BufferedImage changeBGColorSize4PNG(BufferedImage image, Color backgroudColor) {
    	int width = image.getWidth() + 20;
    	int height = image.getHeight() + 20;
    	BufferedImage result = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        Graphics2D graphic = result.createGraphics();
        graphic.setColor(backgroudColor);
        graphic.fillRect(0, 0, width, height);
        graphic.drawImage(image, 10, 10, backgroudColor, null);
        graphic.dispose();
        return result;
      }
}
